package com.xinxin.o2o.controller.productcontroller;

import com.xinxin.o2o.dto.ProductExecution;
import com.xinxin.o2o.enums.GlobalCode;
import com.xinxin.o2o.enums.ProductStateEnum;
import com.xinxin.o2o.enums.ProductStatusEnum;
import com.xinxin.o2o.model.Product;
import com.xinxin.o2o.model.ProductCategory;
import com.xinxin.o2o.model.Shop;
import com.xinxin.o2o.response.Response;
import com.xinxin.o2o.service.ProductService;
import com.xinxin.o2o.util.ResponseUtil;
import com.xinxin.o2o.vo.product.SaleParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @author 史鑫鑫
 * @date 2019-08-20 22:59
 */
@Api(tags = "商品管理模块")
@RestController
@RequestMapping("/api/product")
public class ProductManagementController {

    @Autowired
    private ProductService productService;

    @ApiOperation(value = "获取商品列表")
    @GetMapping("/getProductList")
    public Response getProductList(@RequestParam("shopId") Long shopId,
                                   @RequestParam(value = "productCategoryId", required = false) Long productCategoryId,
                                   @RequestParam(value = "productName", required = false) String productName,
                                   @RequestParam(value = "page", defaultValue = "1") int page,
                                   @RequestParam(value = "size", defaultValue = "10") int size) {
        if (page < 1 || size < 0) {
            return ResponseUtil.error(GlobalCode.PARAM_ERROR, "page 和 size 必须大于 0 ");
        }
        Product productCondition = new Product();
        Shop shop = new Shop();
        shop.setShopId(shopId);
        productCondition.setShop(shop);

        if (productCategoryId != null) {
            ProductCategory productCategory = new ProductCategory();
            productCategory.setProductCategoryId(productCategoryId);
            productCondition.setProductCategory(productCategory);
        }
        if (productName != null) {
            productCondition.setProductName(productName);
        }
        ProductExecution productExecution = productService.getProductList(productCondition, page, size);
        return ResponseUtil.success(productExecution.getProductList());
    }

    /**
     * 添加商品
     *
     * @param productName         商品名称
     * @param productDesc         商品描述
     * @param productCategoryId   分类id
     * @param priority            优先级
     * @param normalPrice         原价
     * @param promotionPrice      折扣价
     * @param productImg          缩略图
     * @param productDetailImages 详情图
     * @return 注册结果
     */
    @ApiOperation("添加商品")
    @PostMapping("/addProduct")
    public Response addProduct(@RequestParam Long shopId,
                               @RequestParam String productName,
                               @RequestParam String productDesc,
                               @RequestParam Long productCategoryId,
                               @RequestParam(required = false) Integer priority,
                               @RequestParam(required = false) String normalPrice,
                               @RequestParam String promotionPrice,
                               @RequestParam MultipartFile productImg,
                               @RequestParam List<MultipartFile> productDetailImages) {
        Product product = new Product();

        Shop shop = new Shop();
        shop.setShopId(shopId);
        product.setShop(shop);

        ProductCategory productCategory = new ProductCategory();
        productCategory.setProductCategoryId(productCategoryId);
        product.setProductCategory(productCategory);

        product.setProductName(productName);
        product.setProductDesc(productDesc);
        product.setPriority(priority == null ? 1 : priority);
        product.setNormalPrice(normalPrice);
        product.setPromotionPrice(promotionPrice);

        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());

        ProductExecution productExecution = productService.addProduct(product, productImg, productDetailImages);
        return ResponseUtil.success(productExecution);
    }

    /**
     * 添加商品
     *
     * @param productName         商品名称
     * @param productDesc         商品描述
     * @param productCategoryId   分类id
     * @param priority            优先级
     * @param normalPrice         原价
     * @param promotionPrice      折扣价
     * @param productImg          缩略图
     * @param productDetailImages 详情图
     * @return 注册结果
     */
    @ApiOperation("修改商品信息")
    @PostMapping("/modifyProduct")
    public Response modifyProduct(@RequestParam Long shopId,
                                  @RequestParam Long productId,
                                  @RequestParam(required = false) String productName,
                                  @RequestParam(required = false) String productDesc,
                                  @RequestParam(required = false) Long productCategoryId,
                                  @RequestParam(required = false) Integer priority,
                                  @RequestParam(required = false) String normalPrice,
                                  @RequestParam(required = false) String promotionPrice,
                                  @RequestParam(required = false) MultipartFile productImg,
                                  @RequestParam(required = false) List<MultipartFile> productDetailImages) {
        Product product = productService.getProductById(productId);
        if (!shopId.equals(product.getShop().getShopId())) {
            return ResponseUtil.error(GlobalCode.PARAM_ERROR, "该商品不属于该店铺");
        }
        if (productCategoryId != null) {
            ProductCategory productCategory = new ProductCategory();
            productCategory.setProductCategoryId(productCategoryId);
            product.setProductCategory(productCategory);
        }
        if (productName != null) {
            product.setProductName(productName);
        }
        if (productDesc != null) {
            product.setProductDesc(productDesc);
        }
        if (priority != null) {
            product.setPriority(priority);
        }
        if (normalPrice != null) {
            product.setNormalPrice(normalPrice);
        }
        if (promotionPrice != null) {
            product.setPromotionPrice(promotionPrice);
        }
        product.setUpdateTime(new Date());
        ProductExecution productExecution = productService.modifyProduct(product, productImg, productDetailImages);
        if (productExecution.getState() == ProductStateEnum.SUCCESS.getState()) {
            return ResponseUtil.success(productExecution);
        } else {
            return ResponseUtil.error(productExecution.getState(), productExecution.getStateInfo());
        }
    }

    /**
     * 根据productId获取商品信息
     *
     * @param shopId    店铺id
     * @param productId 商品id
     * @return 商品信息
     */
    @ApiOperation("获取商品信息")
    @GetMapping("/getProductById")
    public Response getProductById(@RequestParam Long shopId,
                                   @RequestParam Long productId) {
        Product product = productService.getProductById(productId);
        if (shopId.equals(product.getShop().getShopId())) {
            return ResponseUtil.success(productService.getProductById(productId));
        } else {
            return ResponseUtil.error(GlobalCode.PARAM_ERROR, "该商品不属于该店铺");
        }
    }

    /**
     * 上下架商品
     *
     * @param saleParam 上下架参数
     * @return 修改结果
     */
    @ApiOperation("上架或者下架商品")
    @PutMapping("/sale")
    public Response onSale(@RequestBody SaleParam saleParam) {
        Product product = productService.getProductById(saleParam.getProductId());
        if (!saleParam.getShopId().equals(product.getShop().getShopId())) {
            return ResponseUtil.error(GlobalCode.PARAM_ERROR, "该商品不属于该店铺");
        }
        product.setEnableStatus(saleParam.getState());
        ProductExecution productStatusEnum = productService.modifyProduct(product, null, null);
        if (productStatusEnum.getState() == ProductStateEnum.SUCCESS.getState()) {
            return ResponseUtil.success();
        } else {
            return ResponseUtil.error(productStatusEnum.getState(), productStatusEnum.getStateInfo());
        }
    }
}
